﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TimeTracker.Provider;

namespace TimeTracker.SkinProvider
{
    public static partial class Extensions
    {
        public static TimeSpan SessionDuration(this TreeNode node) {

            if (node.Tag is ISession) {
                return ((ISession)node.Tag).Duration();
            }

            if (node.Nodes != null) {
                TimeSpan timeSpan = new TimeSpan();
                
                foreach (TreeNode childNode in node.Nodes) {
                    timeSpan += childNode.SessionDuration();
                }

                return timeSpan;
            }

            return default(TimeSpan);
        }

        public static DateTime GetClosestDateTimeFromParent(this TreeNode node)
        {
            TreeNode tempNode = node;
            while (tempNode.Parent != null)
            {
                if (tempNode.Parent.Tag is DateTime)
                {
                    return (DateTime)tempNode.Parent.Tag;
                }
                tempNode = tempNode.Parent;
            }

            return DateTime.MinValue;
        }
    }
}
